package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Goods;
import com.huidao.entity.Order;
import com.huidao.entity.User;
import com.huidao.util.JDBCUtil;


public class OrderDaoImpl implements OrderDao {
	
	Connection conn;
	PreparedStatement ps;
	ResultSet rs;
	@Override
	public boolean addOrder(Connection conn,Order order) {
		String sql = "INSERT INTO `ORDER`(GOODS_ID,GOODS_NUM,USER_ID,ORDER_STATE)VALUES(?,?,?,0)";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, order.getGoods().getGoodsId());
			ps.setInt(2, order.getGoodsNum());
			ps.setLong(3, order.getUser().getUserId());
			int i =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(null, ps, rs);
		}
		return false;
	}

	@Override
	public List<Order> getOrderByBuyer(int startnum, int pageSize, long userId) {
		// TODO Auto-generated method stub
		List<Order> list = new ArrayList<Order>();
		String sql = "SELECT * FROM `ORDER` a " +
				"LEFT JOIN GOODS b ON a.GOODS_ID=b.GOODS_ID " +
				"LEFT JOIN USER c ON b.USER_ID=c.USER_ID " +
				"WHERE a.USER_ID=? LIMIT ?,?";
		try {
			conn =JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.setInt(2, startnum);
			ps.setInt(3, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Order s = new Order();
				s.setOrderId(rs.getLong("ORDER_ID"));
				s.setOrderState(rs.getInt("ORDER_STATE"));
				s.setGoodsNum(rs.getInt("GOODS_NUM"));
				Goods goods = new Goods();
				goods.setGoodsId(rs.getLong("GOODS_ID"));
				goods.setGoodsPrice(rs.getFloat("GOODS_PRICE"));
				goods.setGoodsAccount(rs.getInt("GOODS_ACCOUNT"));
				goods.setGoodsName(rs.getString("GOODS_NAME"));
				User user = new User();
				user.setUserNickname(rs.getString("USER_NICKNAME"));
				user.setUserPhone(Long.parseLong(rs.getString("USER_PHONE")));
				user.setUserShopname(rs.getString("USER_SHOPNAME"));
				goods.setUser(user);
				s.setGoods(goods);
				list.add(s);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int getTotalCountByBuyer(long userId) {
		// TODO Auto-generated method stub
		String sql = "SELECT COUNT(*) FROM `ORDER` WHERE USER_ID=?";
		try {
			conn =JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			rs = ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	@Override
	public boolean updateOrderState(long orderId, int state) {
		// TODO Auto-generated method stub
		String sql = "UPDATE `ORDER` SET ORDER_STATE=? WHERE ORDER_ID=?";
		try {
			conn =JDBCUtil.getConn();
			ps =conn.prepareStatement(sql);
			ps.setInt(1, state+1);
			ps.setLong(2, orderId);
			int i =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}
	
	@Override
	public List<Order> getOrderBySeller(int startnum, int pageSize, long userId) {
		// TODO Auto-generated method stub
		List<Order> list = new ArrayList<Order>();
		String sql = "SELECT * FROM `ORDER` a " +
				"LEFT JOIN GOODS b ON a.GOODS_ID=b.GOODS_ID " +
				"LEFT JOIN USER c ON a.USER_ID=c.USER_ID " +
				"WHERE b.USER_ID=? LIMIT ?,?";
		try {
			conn =JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.setInt(2, startnum);
			ps.setInt(3, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Order s = new Order();
				s.setOrderId(rs.getLong("ORDER_ID"));
				s.setOrderState(rs.getInt("ORDER_STATE"));
				s.setGoodsNum(rs.getInt("GOODS_NUM"));
				Goods goods = new Goods();
				goods.setGoodsId(rs.getLong("GOODS_ID"));
				goods.setGoodsPrice(rs.getFloat("GOODS_PRICE"));
				goods.setGoodsAccount(rs.getInt("GOODS_ACCOUNT"));
				goods.setGoodsName(rs.getString("GOODS_NAME"));
				User user = new User();
				user.setUserNickname(rs.getString("USER_NICKNAME"));
				user.setUserPhone(Long.parseLong(rs.getString("USER_PHONE")));
				s.setGoods(goods);
				s.setUser(user);
				list.add(s);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int getTotalCountBySeller(long userId) {
		// TODO Auto-generated method stub
		String sql = "SELECT COUNT(*) FROM `ORDER` a " +
				" LEFT JOIN GOODS b ON a.GOODS_ID=b.GOODS_ID " +
				" WHERE b.USER_ID=?";
		try {
			conn =JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			rs = ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}
}
